<?php
/**
 * Created by 惠达浪
 * Email: crazys@126.com
 * Date: 2018/11/25
 * Time: 11:17
 */

namespace app\api\controller\v1;


use app\api\controller\BaseController;
use app\api\facade\MesLogic;
use app\api\facade\TokenService;
use app\api\validate\MesValidate;
use app\lib\enum\ErrorCodeEnum;
use app\lib\enum\MesEnum;
use think\Request;

class Mes extends BaseController {
    private $wc_id;

    /**
     * 构造函数中检查是否登录
     *
     * @param Request|null $request
     */
    public function __construct(Request $request = null) {
        parent::__construct($request);
        $this->checkToken();
        $this->wc_id = TokenService::getWorkcenterIdByToken($this->token);
    }

    /**
     * 获取当前任务的工单信息
     *
     * @param int $id 工单id
     */
    public function getWipOrder($id = 0) {

    }

    /**
     * 获取当前任务
     *
     * @return \think\response
     */
    public function getTask() {
        $task = MesLogic::getRecentTask($this->wc_id);
        return $this->returnData($task);
    }

    /**
     * 获取任务列表
     *
     * @return \think\response
     */
    public function getTaskList() {
        $lists = MesLogic::getTaskLists($this->wc_id);
        return $this->returnData($lists);
    }


    /**
     * 获取班组信息
     */
    public function getClass() {

    }

    /**
     * 获取工作中心编号
     */
    public function getWorkcenter() {

    }

    /**
     * 获取当前工序信息
     */
    public function getProcess() {

    }

    /**
     * 获取下一个工序信息
     */
    public function getNextProcess() {

    }

    /**
     * 获取当前工作中心的待完成工序列表
     */
    public function getProcessList() {

    }

    /**
     * 获取当前工序的工单信息
     *
     * @return \think\response
     * @throws \app\lib\exception\ParameterException
     */
    public function getWorksheet() {
        (new MesValidate())->scene('planid')->goCheck();

        $planid = $this->request->get('planid');
        $detail = MesLogic::getWorkSheetDetailById($planid);

        return $this->returnData($detail);
    }

    /**
     * 开始任务
     *
     * @throws \app\lib\exception\ParameterException
     */
    public function postTaskStart() {
        (new MesValidate())->scene('start')->goCheck();

        $rid = $this->request->post('result_id');
        $userIds = $this->request->post('userIds');
        $userIds = is_array($userIds) ? $userIds : explode(',', $userIds);

        //判断当前任务状态
        $rStatus = MesLogic::getResultStatus($rid);
        switch ($rStatus) {
            case MesEnum::RESULT_START:
                $this->error('任务已经开始', ErrorCodeEnum::TASK_START_FAIL);
                break;
            case MesEnum::RESULT_FINISHED:
                $this->error('该任务已经完成', ErrorCodeEnum::TASK_START_FAIL);
                break;
            default:
        }

        $result = MesLogic::taskStart($rid, $userIds);
        if ($result) {
            $this->success('开始任务成功');
        } else {
            $this->error('开始任务失败', ErrorCodeEnum::TASK_START_FAIL);
        }
    }

    /**
     * 报产
     *
     * @throws \app\lib\exception\ParameterException
     */
    public function postResultReport() {
        (new MesValidate())->scene('report')->goCheck();
        $data = $this->request->post();

        //判断是否已经报产
        if (MesLogic::getResultStatus($data['result_id']) != MesEnum::RESULT_START) {
            $this->error('当前任务未开始，或已经报产', ErrorCodeEnum::CANNOT_REPORT);
        }

        $userIds = is_array($data['userIds']) ? $data['userIds'] : explode(',', $data['userIds']);
        $result = MesLogic::reportResult($data['result_id'], $userIds, $data['quantity']);
        if ($result) {
            $this->success('报产成功');
        }
        $this->error('报产失败');
    }
}